/*
 * Copyright the original author or authors.
 * 
 * Licensed under the MOZILLA PUBLIC LICENSE, Version 1.1 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *	  http://www.mozilla.org/MPL/MPL-1.1.html
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.googlecode.milib.display.ui.combobox {
	import com.googlecode.milib.base.IMLBroadcasterListener;
	import com.googlecode.milib.base.MLBroadcastClass;
	import com.googlecode.milib.data.ScrollableArray;
	import com.googlecode.milib.display.service.MLButton;
	import com.googlecode.milib.display.service.MLMC;
	import com.googlecode.milib.display.ui.list.MCList;
	import com.googlecode.milib.display.ui.list.MCListAbstractElement;
	import com.googlecode.milib.display.ui.list.MCListOwner;
	import com.googlecode.milib.display.ui.scroller.MCScrollerWithUpDnButtons;
	import com.googlecode.milib.service.singleton.MouseService;
	import com.googlecode.milib.tools.MLEventsReflector;
	import com.googlecode.milib.util.MLArrayUtil;
	import com.googlecode.milib.vo.MLEventInfo;

	import flash.display.MovieClip;
	import flash.text.TextField;

	/**
	 * @author Marek Brun
	 */
	public class MCComboBox extends MLBroadcastClass implements MCListOwner, IMLBroadcasterListener {
		
		//DATA:	MCComboBoxItemVO //selected item
		public const event_Selected : String = 'Selected';
		
		private var mlmc:MLMC;
		private var mc_list : MovieClip;
		private var mc_title : MovieClip;
		private var btn:MLButton;
		private var model:ScrollableArray;
		private var list:MCList;
		private var elements:MLEventsReflector;
		private var scroller:MCScrollerWithUpDnButtons;
		private var width:Number;
		private var mc_list_bg:MovieClip;
		private var mc_title_tfName:TextField;
		private var selectedData:MCComboBoxItemVO;
		private var mc_title_bg:MovieClip;
		private var isOpened:Boolean;
		private var isAutoSelect:Boolean;
		private var ms:MouseService;
		private var mcBtn:MLButton;
		
		public function MCComboBox(mc:MovieClip, isAutoSelect:Boolean) {
			mlmc=MLMC.forInstance(mc);
			this.isAutoSelect=isAutoSelect;
			var iniWidth:Number=mc.width;
			mc_list=MovieClip(mc.getChildByName('list'));
			mc_title=MovieClip(mc.getChildByName('title'));
			btn=mlmc.getMLButton('btn');
			btn.addListener(this);
			mc_list.scaleX=mc_list.scaleY=1;
			mlmc.mc.scaleX=1;
			mc_list_bg=mlmc.getMC('list.bg');
			
			elements=new MLEventsReflector(false);
			elements.addListener(this);
			list=new MCList(mlmc.getMC('list.list'), this);
			
			scroller=new MCScrollerWithUpDnButtons(mlmc.getMC('list.scroller'), false);
			scroller.setLength(list.getHeight());
			
			mc_title_tfName=mlmc.getTextField('title.tfName');			mc_title_bg=mlmc.getMC('title.bg');
			
			ms=MouseService.getInstance();
			
			mcBtn=MLButton.forInstance(mc);
			mcBtn.disableButtonMode();
			
			setWidth(iniWidth);
			
			isOpened=false;
			drawIsOpened();
		}
		
		public function get mc():MovieClip { return mlmc.mc; };
		
		protected function setIsOpened(opened:Boolean):void {
			if(isOpened==opened){ return; }
			isOpened=opened;
			drawIsOpened();
		}
		
		protected function drawIsOpened():void {
			if(isOpened) {
				mc.addChild(mc_list);
				ms.addListener(this);
			}else{
				mc.removeChild(mc_list);
				ms.removeListener(this);
			}
		}
		
		public function setSelectedItem(data:MCComboBoxItemVO):void {
			selectedData=data;
			mc_title_tfName.htmlText=data.label;
			setIsOpened(false);
		}
		
		public function setSelectedItemByData(data:*):void {
			setSelectedItem(MLArrayUtil.getWhereObjProp(model.getBaseArray(), 'data', data));
		}
		
		public function getSelectedData():MCComboBoxItemVO {
			return selectedData;
		}
		
		public function setWidth(width:Number):void {
			this.width=width;
			list.setWidth(width-scroller.mc.width);
			mc_list_bg.width=width-scroller.mc.width;
			scroller.mc.x=width-scroller.mc.width;
			mc_title_bg.width=width-btn.btn.width;			mc_title_tfName.width=width-btn.btn.width;
			btn.btn.x=width-btn.btn.width;
		}
		
		protected function setListHeight(listHeight:Number):void {
			//TODO
		}
		
		public function setModel(array:ScrollableArray):void {
			model=array;
			list.setModel(model);
			scroller.setScrolled(model);
		}
		
		public function createMCListElement(mc:MovieClip, index:uint):MCListAbstractElement {
			var element:MCComboBoxListElement=new MCComboBoxListElement(mc);
			elements.addObject(element);
			return element;
		}
		
//********************************************************************************************
//		events for MCComboBox
//********************************************************************************************
		public function onEvent(ev:MLEventInfo):void {
			switch(ev.hero){
				case elements:
					if(ev.event!=elements.event_EventReflection){ break; }
					ev=ev.data;
					var hero:MCComboBoxListElement=MCComboBoxListElement(ev.hero);
					switch(ev.event) {
						case hero.event_Select:
							broadcastEvent(event_Selected, hero.getData());
							if(isAutoSelect){
								setSelectedItem(hero.getData());
							}
						break;
					}
				break;
				case btn:
					switch(ev.event) {
						case btn.event_Press:
							setIsOpened(!isOpened);
						break;
					}
				break;
				case ms:
					switch(ev.event) {
						case ms.event_Down:
							if(!mcBtn.getIsOver()){
								setIsOpened(false);
							}
						break;
					}
				break;
			}
		}
		
	}
}
